using System;
using System.IO;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Security.AccessControl;

using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.Windows.Forms;
using System.Threading;

using SypEngine;
using SypEngine.TransferHandler;
using SypConst.Garbage;
using SypEngine.SiteConfig;
using Tamir.SharpSsh;

using SypConst.Path;
using SypServer;

namespace SypEngine
{
    public class SypSynchroniseVisitor : SypVisitor
    {
        public delegate void LockUnlockCallback();
        private event VisitorCallBack evt_synchroniseLock = null;
        private event VisitorCallBack evt_synchroniseUnlock = null;

        #region private methods

        private int[] SmartPictureResizing(int pictOriginalWidth, int pictOriginalHeight , int mode)
        {
            int PictureWidth = Screen.AllScreens[0].WorkingArea.Width - 200;
            int PictureHeight = Screen.AllScreens[0].WorkingArea.Height - 280;

            int WidthReference = (mode == 0) ? SypGarbage.THUMBNAIL_WIDTH : PictureWidth;
            //int WidthReference = (mode == 0) ? SypGarbage.THUMBNAIL_WIDTH : SypGarbage.PICTURE_WIDTH;

            int HeightReference = (mode == 0) ? SypGarbage.THUMBNAIL_HEIGHT : PictureHeight;
            //int HeightReference = (mode == 0) ? SypGarbage.THUMBNAIL_HEIGHT: SypGarbage.PICTURE_HEIGHT;

            if (pictOriginalHeight > pictOriginalWidth)
            {
                int widthResize = (pictOriginalWidth * HeightReference / pictOriginalHeight);
                int heightResize = HeightReference;
                while (widthResize > WidthReference)
                {
                    widthResize -= 50;
                    heightResize = widthResize * pictOriginalHeight / pictOriginalWidth;
                }
                return new int[] { widthResize, heightResize };
            }
            else
            {
                int heightResize = pictOriginalHeight * WidthReference / pictOriginalWidth;
                int widthResize = WidthReference;
                while (heightResize > HeightReference)
                {
                    heightResize -= 50;
                    widthResize = heightResize * pictOriginalWidth / pictOriginalHeight;
                }
                return new int[] { widthResize, heightResize };
                //return new int[] { WidthReference, (pictOriginalHeight * WidthReference / pictOriginalWidth) };
            }
        }


        #endregion


        public SypSynchroniseVisitor(SetText OutputMethod, VisitorCallBack[] Callbacks)
            : base (OutputMethod, Callbacks)
        {
            if (Callbacks.Length > 3)
                evt_synchroniseLock += new VisitorCallBack(Callbacks[3]);
            if (Callbacks.Length > 4)
                evt_synchroniseUnlock += new VisitorCallBack(Callbacks[4]);
        }


        #region Syp Local Commands

        public void VisitClearMethod(SypSiteHandler SypObj, string[] parameters)
        {

            if (parameters.Length > 2)
            {
                int SiteId = -1;
                if (int.TryParse(cleanArg(parameters[1]), out SiteId))
                {
                    SypObj.ClearQueues(SiteId);
                }
                else
                    OutputMethod("Clear-synchronise : cannot retrieve "+SiteId+" action queues", Output.ERROR);
            }

        }

        public void VisitClearfullMethod(SypSiteHandler SypObj, string[] parameters)
        {

            if (parameters.Length > 1)
            {
                int SiteId = -1;
                if (int.TryParse(cleanArg(parameters[1]), out SiteId))
                {
                    SypObj.RemoveSyncTable(SiteId);
                }
                else
                    OutputMethod("Clearfull-synchronise : cannot retrieve " + SiteId + " action queues", Output.ERROR);
            }

        }

        public void VisitClearfullallMethod(SypSiteHandler SypObj, string[] parameters)
        {
            SypObj.RemoveSyncTables();
        }

        public void VisitClearallMethod(SypSiteHandler SypObj, string[] parameters)
        {
            SypObj.ClearQueues();
        }

        public void VisitLocalallMethod(SypSiteHandler SypObj, string[] parameters)
        {
            int cpt = 0;
            List<int> listSiteIds = SypObj.getSiteIds();
            if (listSiteIds.Count > 0)
            {
                foreach (int key in listSiteIds)
                {
                    OutputMethod(new object[] { cpt, listSiteIds.Count }, Output.PROGRESS_POS);
                    VisitLocalMethod(SypObj, new string[] { "", key.ToString() });
                    cpt++;
                }
                OutputMethod(string.Empty, Output.PROGRESS_RESET_POS);

            }
            else
                OutputMethod("LocalAll-synchronise : action queues are empties.", Output.ERROR);

        }

        public void VisitLocalMethod(SypSiteHandler SypObj, string[] parameters)
        {
            if (parameters.Length > 1)
            {
                int SiteId = 0;

                try
                {
                    SiteId = int.Parse(cleanArg(parameters[1]));
                }
                catch (Exception)
                {
                    OutputMethod("Synchronise-local : parameter is not a number", Output.ERROR);
                    return;
                }
                try
                {
                    evt_synchroniseLock(SiteId);
                    if (SypObj.SyncExists(SiteId))
                    {
                        SypSyncTable QueueTable = (SypSyncTable)SypObj.GetSync(SiteId);
                        if (QueueTable.LocalQueue != null)
                        {
                            ImageCodecInfo ici = null;
                            foreach (ImageCodecInfo codec in ImageCodecInfo.GetImageEncoders())
                                if (codec.MimeType == "image/jpeg")
                                    ici = codec;

                            bool SerializeConf = true;
                            int cpt = 1;
                            int InitQueueCount = QueueTable.LocalQueue.Count;
                            while (QueueTable.LocalQueue.Count > 0 && !HasTerminatedRequest())
                            {
                                SypActionObj SAObj = (SypActionObj)QueueTable.LocalQueue.Dequeue();
                                switch (SAObj.Action)
                                {
                                    case SyncAction.COPY_FILE:
                                        {
                                            if (SAObj.SypObj != null)
                                            {
                                                if (SAObj.SypObj is string[])
                                                {
                                                    if (((string[])SAObj.SypObj).Length > 1)
                                                    {
                                                        string srcFile = ((string[])SAObj.SypObj)[0];
                                                        string destFile = ((string[])SAObj.SypObj)[1];


                                                        if (File.Exists(srcFile))
                                                            File.Copy(srcFile, destFile, true);

                                                        else
                                                            OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : copy failed from (" + srcFile + ") to (" + destFile + ")", Output.ERROR);
                                                    }
                                                    else
                                                        OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : copy failed, missing parameters", Output.ERROR);

                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : try to add an unknown object", Output.ERROR);
                                            }
                                            break;
                                        }

                                    case SyncAction.COPY_PICTURE:
                                        {
                                            if (SAObj.SypObj != null)
                                            {
                                                if (SAObj.SypObj is string[])
                                                {
                                                    if (((string[])SAObj.SypObj).Length > 1)
                                                    {
                                                        string _path = cleanArg(((string[])SAObj.SypObj)[0]);
                                                        if (File.Exists(_path))
                                                        {

                                                            //string ThumbFilename = SypGarbage.TMP_FOLDER +  SiteId + "/" + SypGarbage.LOCAL_FOLDER + SypGarbage.THUMBNAILS_FOLDER + SypGarbage.THUMBNAIL_PREF + ((string[])SAObj.SypObj)[1] + SypGarbage.THUMBNAIL_EXTENSION;
                                                            string ThumbFilename = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.THUMBNAIL_FILE, null, SiteId, -1, -1, int.Parse(((string[])SAObj.SypObj)[1]));
                                                            //string PictFilename = SypGarbage.TMP_FOLDER + SiteId + "/" + SypGarbage.LOCAL_FOLDER + SypGarbage.PICTURES_FOLDER + SypGarbage.PICTURE_PREF + ((string[])SAObj.SypObj)[1] + SypGarbage.PICTURE_EXTENSION;
                                                            string PictFilename = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.PICTURE_FILE, null, SiteId, -1, -1, int.Parse(((string[])SAObj.SypObj)[1]));

                                                            if (File.Exists(ThumbFilename))
                                                                File.Delete(ThumbFilename);

                                                            if (File.Exists(PictFilename))
                                                                File.Delete(PictFilename);

                                                            //System.Drawing.Imaging.Encoder enc = System.Drawing.Imaging.Encoder.Compression;
                                                            EncoderParameters epsThumb = new EncoderParameters(1);
                                                            EncoderParameters epsOrig = new EncoderParameters(1);
                                                            epsThumb.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, SypGarbage.JPEG_COMPRESSION_QUALITY_THUMB);
                                                            epsOrig.Param[0] = new EncoderParameter(System.Drawing.Imaging.Encoder.Compression, SypGarbage.JPEG_COMPRESSION_QUALITY_ORIGINAL);

                                                            //create thumbnail
                                                            Image oImg = Image.FromFile(_path);
                                                            int[] sizes = SmartPictureResizing(oImg.Width, oImg.Height, 0);
                                                            Image oThumbNail = new Bitmap(oImg, new Size(sizes[0], sizes[1]));
                                                            //oThumbNail.Save(ThumbFilename, ImageFormat.Jpeg);
                                                            oThumbNail.Save(ThumbFilename, ici, epsThumb);
                                                            SypGarbage.free(oThumbNail);

                                                            //create picture
                                                            oImg = Image.FromFile(_path);
                                                            sizes = SmartPictureResizing(oImg.Width, oImg.Height, 1);
                                                            Image oPictNail = new Bitmap(oImg, new Size(sizes[0], sizes[1]));
                                                            //oThumbNail.Save(PictFilename, ImageFormat.Jpeg);
                                                            oPictNail.Save(PictFilename, ici, epsOrig);
                                                        }
                                                        else
                                                            OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : cannot find picture", Output.ERROR);
                                                    }
                                                    else
                                                        OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : missing parameters", Output.ERROR);
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : try to add an unknown object", Output.ERROR);
                                            }
                                            break;
                                        }


                                    case SyncAction.ADD_FILE:
                                        {
                                            if (SAObj.SypObj != null)
                                            {
                                                if (SAObj.SypObj is SypPage)
                                                {
                                                    ((SypPage)SAObj.SypObj).isLocal = 0;
                                                    ((SypPage)SAObj.SypObj).LoadPage(SypObj.GetSiteConfig(SiteId));
                                                    ((SypPage)SAObj.SypObj).GenerateSypPage();
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : try to add an unknown object", Output.ERROR);
                                            }
                                            break;
                                        }
                                    case SyncAction.ADD_FOLDER:
                                        {
                                            if (SAObj.SypObj != null && SAObj.SypObj is string)
                                            {
                                                if (!Directory.Exists(SAObj.SypObj.ToString()))
                                                    //Directory.Delete(SAObj.SypObj.ToString(), true);
                                                    Directory.CreateDirectory(SAObj.SypObj.ToString());
                                            }
                                            else
                                                OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : cannot create directory, " + SAObj.SypObj.ToString(), Output.ERROR);
                                            break;
                                        }
                                    case SyncAction.SUPPR_CONF:
                                    case SyncAction.SUPPR_FILE:
                                        {
                                            if (SAObj.Action == SyncAction.SUPPR_CONF)
                                                SerializeConf = false;


                                            if (SAObj.SypObj != null && SAObj.SypObj is string)
                                            {
                                                if (File.Exists(SAObj.SypObj.ToString()))
                                                    File.Delete(SAObj.SypObj.ToString());
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : file not found, " + SAObj.SypObj.ToString(), Output.ERROR);
                                            }
                                            else
                                                OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : cannot delete file, " + SAObj.SypObj.ToString(), Output.ERROR);
                                            break;
                                        }
                                    case SyncAction.SUPPR_FOLDER:
                                        {
                                            if (SAObj.SypObj != null && SAObj.SypObj is string)
                                            {
                                                if (Directory.Exists(SAObj.SypObj.ToString()))
                                                    Directory.Delete(SAObj.SypObj.ToString(), true);
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : directory not found, " + SAObj.SypObj.ToString(), Output.ERROR);
                                            }
                                            else
                                                OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Local-synchronise : cannot delete directory, " + SAObj.SypObj.ToString(), Output.ERROR);
                                            break;
                                        }

                                }
                                SypGarbage.free(SAObj);
                                OutputMethod(JsonResponse.GetJsonResponse(SiteId, "localStep", new Object[] { cpt, InitQueueCount }), Output.JSON);
                                cpt++;
                            }

                            if (SerializeConf)
                            {
                                SypObj.SerializeSite(SiteId);
                                OutputMethod(JsonResponse.GetJsonResponse(SiteId, "updateDone", null), Output.JSON);
                            }
                            else
                            {
                                OutputMethod(JsonResponse.GetJsonResponse(SiteId, "deleteDone", null), Output.JSON);
                            }
                            //SypGarbage.SaveSypConf();

                        }
                    }
                    else
                        OutputMethod("[" + SiteId + "]Local-synchronise : any command to execute", Output.OUT);
                }
                catch (Exception e)
                {
                    OutputMethod("[" + SiteId + "]Local-synchronise error catched : "+e.ToString(), Output.ERROR);
                }
                finally {
                    evt_synchroniseUnlock(SiteId);
                }
            }
            else
                OutputMethod("Local-synchronise : missing parameters", Output.ERROR);
        }

        #endregion

        #region Syp Online Commands

        public void VisitOnlineallMethod(SypSiteHandler SypObj, string[] parameters)
        {
            try
            {
                List<int> listSiteIds = SypObj.getSiteIds();
                if (listSiteIds.Count > 0)
                {
                    int cpt = 0;
                    foreach (int key in SypObj.getSiteIds())
                    {
                        OutputMethod(new object[] { cpt, listSiteIds.Count }, Output.PROGRESS_POS);
                        VisitOnlineMethod(SypObj, new string[] { "", key.ToString() });
                        cpt++;
                    }
                    OutputMethod(String.Empty, Output.PROGRESS_RESET_POS);
                }
                else
                    OutputMethod("OnlineAll-synchronise : action queues are empties.", Output.ERROR);
            }
            catch (Exception e)
            {
                OutputMethod("OnlineAll-synchronise : error catched, " + e.ToString(), Output.ERROR);
            }
            finally
            {
                OutputMethod(String.Empty, Output.ONLINE_ALL_DONE);
            }
        }

        public void VisitCheckMethod(SypSiteHandler SypObj, string[] parameters)
        {
            SypTransferHandler SypTransfer = null;

            if (NetworkAvailable())
            {
                if (parameters.Length > 1)
                {
                    int SiteId = -1;
                    if (!int.TryParse(cleanArg(parameters[1]), out SiteId))
                    {
                        OutputMethod("Check-synchronise : parameter is not a number", Output.ERROR);
                        return;
                    }
                    SypSite mySite = SypObj.GetSiteConfig(SiteId);

                    if (mySite != null)
                    {
                        try
                        {

                            TransferConf SiteConf = mySite.SypTfConf;
                            SypTransfer = SypTransferHandler.BuildSypTransfer(OutputMethod, mySite.SiteConnectionMode, SiteConf);

                            int[] synchronizedListIds = SypTransfer.CheckSynchronizedPictures(SiteConf.RemoteDir);
                            OutputMethod(JsonResponse.GetJsonResponse(SiteId, "checkSynchronized", new object[] { synchronizedListIds }), Output.JSON);
                        }
                        catch (Exception e)
                        {
                            OutputMethod("[" + SiteId + "] Check-synchronise error :" + e.ToString(), Output.ERROR);
                        }
                        finally
                        {
                            if (SypTransfer != null)
                                SypTransfer.Close();
                        }
                    }
                }

            }
        }

        public void VisitOnlineMethod(SypSiteHandler SypObj, string[] parameters)
        {
            SypTransferHandler SypTransfer = null;

            if (NetworkAvailable())
            {
                if (parameters.Length > 1)
                {
                    int SiteId = -1;
                    if (!int.TryParse(cleanArg(parameters[1]), out SiteId))
                    {
                        OutputMethod("Online-synchronise : parameter is not a number", Output.ERROR);
                        return;
                    }


                    SypSite mySite = SypObj.GetSiteConfig(SiteId);

                    if (mySite != null)
                    {
                        int cpt = 1;
                        Queue<SypActionObj> CopyOnlineQueue = null;
                        Queue<SypActionObj> SypTransferErrorToRetryQueue = new Queue<SypActionObj>();
                        try
                        {
                            //OutputMethod(GetRessources("ProgressPublishing") + " " + mySite.SiteTitle, Output.PROGRESS_STATUS);
                            TransferConf SiteConf = mySite.SypTfConf;
                            SypTransfer = SypTransferHandler.BuildSypTransfer(OutputMethod, mySite.SiteConnectionMode, SiteConf);


                            if (SiteConf.RemoteDir != null && SiteConf.RemoteDir != "" && SiteConf.RemoteDir != "/")
                            {
                                SypTransfer.MakeDir(SiteConf.RemoteDir);
                                SypTransfer.SetCurrentDirectory(SiteConf.RemoteDir);
                            }

                            //Queue action is copied to avoid cross threading conflicts
                            CopyOnlineQueue = SypObj.CopyOnlineQueue(SiteId);

                            int totalCount = CopyOnlineQueue.Count;
                            while (CopyOnlineQueue != null && CopyOnlineQueue.Count > 0 && !HasTerminatedRequest())
                            {
                                SypActionObj SAObj = null;
                                try
                                {
                                    SAObj = CopyOnlineQueue.Dequeue();
                                    string pathForProgressBar = null;
                                    string nameForProgressBar = null;
                                    string extForProgressBar = null;

                                    switch (SAObj.Action)
                                    {
                                        case SyncAction.ADD_ZIP:
                                            {
                                                if (SAObj.SypObj != null && SAObj.SypObj is string)
                                                {
                                                    string Remotefile = pathForProgressBar = (string)SAObj.SypObj;
                                                    //string LocalFile = SypGarbage.TMP_FOLDER + SiteId + "/" + SypGarbage.LOCAL_FOLDER + SypGarbage.ZIP_FOLDER + SypGarbage.GetFileName(Remotefile);
                                                    string LocalFile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.ZIP_FOLDER, null, SiteId) + '/' + SypGarbage.GetFileName(Remotefile);

                                                    FileInfo FI = new FileInfo(LocalFile);
                                                    nameForProgressBar = FI.Name;
                                                    extForProgressBar = FI.Extension;

                                                    if (File.Exists(LocalFile))
                                                    {
                                                        SypTransfer.SendFile(LocalFile, Remotefile);
                                                        //File.Delete(LocalFile);
                                                    }
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Online-synchronise : cannot create zip file, " + SAObj.SypObj.ToString(), Output.ERROR);
                                                break;
                                            }

                                        case SyncAction.ADD_FILE:
                                            {
                                                if (SAObj.SypObj != null && SAObj.SypObj is string)
                                                {
                                                    string Remotefile = pathForProgressBar = (string)SAObj.SypObj;
                                                    //string LocalFile = SypGarbage.TMP_FOLDER + SiteId + "/" + SypGarbage.TO_UP_FOLDER + SypGarbage.GetFileName(Remotefile);
                                                    string LocalFile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.TOUP_FOLDER, null, SiteId) + '/' + SypGarbage.GetFileName(Remotefile);

                                                    FileInfo FI = new FileInfo(LocalFile);
                                                    nameForProgressBar = FI.Name;
                                                    extForProgressBar = FI.Extension;

                                                    if (File.Exists(LocalFile))
                                                    {
                                                        SypTransfer.SendFile(LocalFile, Remotefile);
                                                        File.Delete(LocalFile);
                                                    }
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Online-synchronise : cannot create file, " + SAObj.SypObj.ToString(), Output.ERROR);
                                                break;
                                            }
                                        case SyncAction.ADD_FOLDER:
                                            {
                                                if (SAObj.SypObj != null && SAObj.SypObj is string)
                                                {
                                                    SypTransfer.MakeDir(((string)SAObj.SypObj));
                                                    pathForProgressBar = nameForProgressBar = (string)SAObj.SypObj;
                                                    extForProgressBar = "folder";
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Online-synchronise : cannot create folder, " + SAObj.SypObj.ToString(), Output.ERROR);
                                                break;
                                            }
                                        case SyncAction.SUPPR_FOLDER:
                                            {
                                                if (SAObj.SypObj != null && SAObj.SypObj is string)
                                                {
                                                    SypTransfer.RemoveDir(((string)SAObj.SypObj));
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Online-synchronise : cannot delete directory, " + SAObj.SypObj.ToString(), Output.ERROR);
                                                break;

                                            }
                                        case SyncAction.SUPPR_FILE:
                                            {
                                                if (SAObj.SypObj != null && SAObj.SypObj is string)
                                                {
                                                    SypTransfer.DeleteFile(((string)SAObj.SypObj));
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Online-synchronise : cannot delete file, " + SAObj.SypObj.ToString(), Output.ERROR);
                                                break;
                                            }
                                        case SyncAction.COPY_PICTURE:
                                            {
                                                if (SAObj.SypObj != null && SAObj.SypObj is string)
                                                {
                                                    string Remotefile = pathForProgressBar = (string)SAObj.SypObj;
                                                    //string LocalFile = SypGarbage.TMP_FOLDER + SiteId + "/" + SypGarbage.LOCAL_FOLDER + (string)SAObj.SypObj;
                                                    string LocalFile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.ROOT_FOLDER, null, SiteId) + '/' + (string)SAObj.SypObj;

                                                    FileInfo FI = new FileInfo(LocalFile);
                                                    nameForProgressBar = FI.Name;
                                                    extForProgressBar = FI.Extension;
                                                    SypTransfer.SendFile(LocalFile, Remotefile);
                                                    if (FI.Name.Contains(SypPathHandler.PICTURE_PREF))
                                                    {
                                                        string id = nameForProgressBar.Replace(SypPathHandler.PICTURE_PREF, string.Empty).Replace(FI.Extension, string.Empty);
                                                        OutputMethod(JsonResponse.GetJsonResponse(SiteId, "checkSynchronizedFlag", new object[] { id }), Output.JSON);
                                                    }
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Online-synchronise : cannot copy picture file, " + SAObj.SypObj.ToString(), Output.ERROR);

                                                break;
                                            }
                                        case SyncAction.ADD_INDEX:
                                            {
                                                if (SAObj.SypObj != null && SAObj.SypObj is string)
                                                {
                                                    string Remotefile = pathForProgressBar = (string)SAObj.SypObj;
                                                    //string LocalFile = SypGarbage.TMP_FOLDER + SiteId + "/" + SypGarbage.LOCAL_FOLDER + SypGarbage.DEFAULT_PAGE;
                                                    string LocalFile = SypPathHandler.GetPath(SypPathMode.LOCAL, SypPathRef.BO_PAGE, null, SiteId);

                                                    FileInfo FI = new FileInfo(LocalFile);
                                                    nameForProgressBar = FI.Name;
                                                    extForProgressBar = FI.Extension;

                                                    SypTransfer.SendFile(LocalFile, Remotefile);
                                                }
                                                else
                                                    OutputMethod("[" + SiteId + "][" + SAObj.Action + "]Online-synchronise : cannot copy picture file, " + SAObj.SypObj.ToString(), Output.ERROR);

                                                break;
                                            }
                                        default:
                                            {
                                                break;
                                            }


                                    }
                                    if (pathForProgressBar != null && nameForProgressBar != null && extForProgressBar != null)
                                    {
                                        OutputMethod(JsonResponse.GetJsonResponse(SiteId, "onlineStep", new Object[] { cpt, totalCount, nameForProgressBar, pathForProgressBar, extForProgressBar }), Output.JSON);
                                    }
                                    else
                                    {
                                        OutputMethod(JsonResponse.GetJsonResponse(SiteId, "onlineStep", new Object[] { cpt, totalCount }), Output.JSON);
                                    }
                                }
                               /* catch (SypTransferException)
                                {
                                    if (SAObj.CanRetry())
                                    {
                                        SypTransferErrorToRetryQueue.Enqueue(SAObj);
                                    }
                                }*/
                                catch (Exception e)
                                {
                                    if (SAObj.CanRetry())
                                    {
                                        SypTransferErrorToRetryQueue.Enqueue(SAObj);
                                    }
                                    //OutputMethod("[" + SiteId + "][" + mySite.SiteConnectionMode + "]Online-synchronise : we meet the following exception while processing transfer, " + e.ToString(), Output.ERROR);
                                }
                                finally
                                {
                                    cpt++;
                                }
                            }
                            int[] synchronizedListIds = SypTransfer.CheckSynchronizedPictures(SiteConf.RemoteDir);
                            OutputMethod(JsonResponse.GetJsonResponse(SiteId, "checkSynchronized", new object[] { synchronizedListIds }), Output.JSON);

                        }
                        catch (Exception e)
                        {
                            OutputMethod("[" + SiteId + "]Online-synchronise : exception occured at initializing connection, " + e.ToString(), Output.ERROR);
                        }
                        finally
                        {
                            if (SypTransferErrorToRetryQueue != null && SypTransferErrorToRetryQueue.Count > 0)
                            {
                                SypObj.RevertToOnlineQueue(SiteId, new List<SypActionObj>(SypTransferErrorToRetryQueue));
                            }
                            if (CopyOnlineQueue != null && CopyOnlineQueue.Count > 0)
                            {
                                SypObj.RevertToOnlineQueue(SiteId, new List<SypActionObj>(CopyOnlineQueue));
                            }
                            SypGarbage.free(CopyOnlineQueue);
                            OutputMethod(JsonResponse.GetJsonResponse(SiteId, "onlineDone", null), Output.JSON);
                            if (SypTransfer != null)
                                SypTransfer.Close();
                        }
                    }
                }
                else
                    OutputMethod("Online-synchronise : missing parameters", Output.ERROR);
                //OutputMethod(string.Empty, Output.UNLOCK);
            }
            else
                OutputMethod("Online-synchronise : network connecion not available", Output.ERROR);
        }

        #endregion


    }
}
